package com.jy.modules.cims.component.common;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jy.modules.cims.common.CimsConstant;
import com.jy.modules.cims.common.sysUtils.CollectionsUtils;
import com.jy.modules.cims.common.sysUtils.SimpleStringUtils;
import com.jy.modules.cims.component.common.service.TCMmExcelTemplateService;
import com.jy.modules.cims.data.common.dao.TCMmLoanDetailDao;
import com.jy.modules.platform.sysdict.impl.SysDictAPImpl;
import com.jy.platform.api.sysdict.SysDictDetailVo;

/**
 * 交易字典转换
 */
@Service("com.jy.modules.cims.component.common.DictDeatil")
public class DictDeatil{
	@Autowired
	private TCMmExcelTemplateService serviceExcelTemplate;
	
	@Autowired
	private TCMmLoanDetailDao dao;
	
	@Autowired
	private SysDictAPImpl sysDictAPI;
	
	/**
	 * 数据字典码值取名称
	 * 
	 * @Title getDictDetailName
	 * @Description TODO
	 * @param dict_code
	 * @param detail_value
	 * @return String
	 * @author zhoupeng
	 * @date 2015-4-9-下午4:40:07
	 * @update
	 * @throws
	 */
	public String getDictDetailName(String dict_code, String detail_value) {
		String dictDetailName = "";
		if (!SimpleStringUtils.isBlank(dict_code) && !SimpleStringUtils.isBlank(detail_value)) {
			SysDictDetailVo sysDictDetailVo = sysDictAPI.queryDetailByDictCodeAndDeatailValue(dict_code, detail_value);
			if (sysDictDetailVo != null)
				dictDetailName = sysDictDetailVo.getDictDetailName();
		}
		return dictDetailName;
	}
	
	public Map<String, Map> queryDictDetailByTempalteMap(String[] columnCodes) throws Exception {
		// String columnCodes[]=null;
		Map<String, Map> excelColumnsConvertMap = new HashMap<String, Map>();

		if (null == columnCodes || columnCodes.length == 0) {
			return excelColumnsConvertMap;
		}

		for (int index = 0; index < columnCodes.length; index++) {
			String dictCode = columnCodes[index];

			if (SimpleStringUtils.hasText(dictCode)) {
				Map<String, String> dictDataDetail = this.getDictDetailValue(dictCode);
				excelColumnsConvertMap.put(dictCode, dictDataDetail);
			}
		}
		return excelColumnsConvertMap;
	}
	    
	    /**
		 * 数据字典码值取名称
		 * 
		 * @Title getDictDetailValue
		 * @Description TODO
		 * @param dictCode
		 * @return Map<String, String>
		 * @throws
		 */
	private Map<String, String> getDictDetailValue(String dictCode) {

		List<Map> dictData = null;
		Map<String, String> dictDataDetail = new HashMap<String, String>();
		if (!SimpleStringUtils.isBlank(dictCode)) {
			// {ORDERBY=1, DICVALUE=1, DICNAME=资金周转}
			dictData = sysDictAPI.getDictByKey(dictCode);
			if (CollectionsUtils.isNotEmpty(dictData)) {
				for (int index = 0; index < dictData.size(); index++) {
					Map dictMap = dictData.get(index);
					// dictDataDetail.put(dictMap.get(CimsConstant.DICT_DATA_NAME).toString(),
					// dictMap.get(CimsConstant.DICT_DATA_CODE).toString());
					dictDataDetail.put(dictMap.get(CimsConstant.DICT_DATA_CODE) == null ? "" : dictMap.get(CimsConstant.DICT_DATA_CODE).toString(),
									dictMap.get(CimsConstant.DICT_DATA_NAME)== null ? "" : dictMap.get(CimsConstant.DICT_DATA_NAME).toString());
				}
			}

		}
		return dictDataDetail;
	}
	    
}
